<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="handleBatchForceLogout">
                <Icon icon="i-ant-design:delete-outlined" />
                强退
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="i-mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <Detail @register="registerDetail" />
  </div>
</template>

<script lang="ts" name="online-user" setup>
  // ts语法
  import { ref, computed, unref } from "vue"
  import { BasicTable, TableAction, ActionItem } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import { useListPage } from "/@/hooks/system/useListPage"
  import Detail from "./Detail.vue"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { columns, searchFormSchema } from "./user.data"
  import * as userApi from "./api"
  import { Icon } from "/@/components/Icon"

  // import { usePermission } from '/@/hooks/web/usePermission'
  // const { hasPermission } = usePermission();

  const { createMessage, createConfirm } = useMessage()
  const [registerDetail, { openDrawer: openDetail }] = useDrawer()

  const { prefixCls, tableContext } = useListPage({
    designScope: "online-user-list",
    tableProps: {
      title: "在线用户列表",
      api: userApi.page,
      columns: columns,
      alwaysShowTitle: true,
      size: "small",
      bordered: false,
      formConfig: {
        labelWidth: 90,
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 120,
      },
      beforeFetch: (params) => {
        return Object.assign({ column: "loginTime", order: "desc" }, params)
      },
    }
  })

  const [registerTable, { reload, updateTableDataRecord }, { rowSelection, selectedRowKeys }] = tableContext

  async function handleForceLogout(record: Recordable) {
    console.log(record)
    await userApi.forceLogout({ id: record.id }, reload)
  }

  const handleOpenDetails = (record) => openDetail(true, { record })

  async function handleBatchForceLogout() {
    await userApi.forceLogoutBatch({ ids: selectedRowKeys.value }, () => {
      selectedRowKeys.value = []
      reload()
    })
  }

  function getTableAction(record): ActionItem[] {
    return [
      {
        label: "详情",
        onClick: handleOpenDetails.bind(null, record),
      },
      {
        label: "强退",
        color: 'error',
        auth: 'auth:ForceLogoutOnlineUser',
        onClick: handleForceLogout.bind(null, record),
      },
    ]
  }

  function getDropDownAction(record): ActionItem[] {
    return []
  }
</script>

<style scoped></style>
